package com.vision.util.redis.single;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Filename RedisDao.java
 * @Description
 * @Version 1.0
 * @Author zhongc
 * @Email zhong_ch@foxmail.com
 * @History <li>Author: zhongc</li>
 * <li>Date: 2017/1/21 0021</li>
 * <li>Version: 1.0</li>
 * <li>Content: create</li>
 */
public class RedisDao {

    Logger log = Logger.getLogger(getClass().getName());

    private JedisPool jedisPool;

    public RedisDao(JedisPool jedisPool) {
        if (jedisPool == null) {
            throw new IllegalArgumentException("please config jedisPool");
        }
        this.jedisPool = jedisPool;
    }

    public String hmset(String key, Map<String, String> hash) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hmset(key, hash);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    public Set<String> keys(String key) {
        Jedis jedis = null;
        Set<String> set = Sets.newHashSet();
        try {
            jedis = jedisPool.getResource();
            set = jedis.keys(key);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return set;
    }

    public Map<String, String> hgetAll(String key) {
        Jedis jedis = null;
        Map<String, String> map = Maps.newHashMap();
        try {
            jedis = jedisPool.getResource();
            map = jedis.hgetAll(key);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return map;
    }

    public Long lpush(String key, String... string) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.lpush(key, string);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    public String rpop(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.rpop(key);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    public Long del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (JedisConnectionException e) {
            log.log(Level.WARNING, "链接超时", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
}
